package ${serviceImplUrl};

import com.baoyuan.basic.framework.core.util.BeanUtil;
import ${entityUrl}.${entityName};
import ${entityResponseUrl}.${entityResponseName};
import ${daoUrl}.${entityName}${daoClassNameSuffix};
import ${serviceUrl}.${entityName}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baoyuan.crm.handler.DataSourceExecute;
<#list pkgs as ps>
    <#if ps??>
        import ${ps};
    </#if>
</#list>
import java.util.List;

/**
 * 
 */
@Service
public class ${entityName}ServiceImpl implements ${entityName}Service  {
    @Autowired
    private ${entityName}Mapper mapper;

    @Override
    public ${entityName} findEntityById(Long id) {
        ${entityName} entity = DataSourceExecute.businessRun(() -> {
            return mapper.selectByPrimaryKey(id);
        });
        return entity;
    }

    @Override
    public ${entityResponseName} findResponseById(Long id) {
        ${entityName} entity = findEntityById(id);
        ${entityResponseName} response = BeanUtil.copy(entity, ${entityResponseName}.class);
        return response;
    }

    @Override
    public Long saveEntity(${entityName} param) {
        Long id = DataSourceExecute.businessRun(() -> {
            mapper.insert(param);
            return param.getId();
        });
        return id;
    }

    // 如需要事务处理，请放开这行注释  @TargetDataSourceByTX(txname = CrmConstants.BUSINESS_TX, dbname = CrmConstants.BUSINESS, rollbackFor = Throwable.class)
    @Override
    public Long save(${methodParams}) {
        ${entityName} param = new ${entityName}();
        Date now = new Date();
        <#list cis as ci>
            <#if ci.property == "isDeleted">
        param.setIsDeleted(Boolean.FALSE);
            <#elseif ci.property == "creatorId" || ci.property == "createId" || ci.property == "updateId" || ci.property == "updatorId">
        param.${ci.propertySetMethod}(operatorId);
            <#elseif ci.property == "createTime" || ci.property == "updateTime">
        param.${ci.propertySetMethod}(now);
            <#else >
        param.${ci.propertySetMethod}(${ci.property});
            </#if>
        </#list>
        param.setId(id);
        return this.saveEntity(param);
    }

    @Override
    public Boolean updateByPrimaryKeySelective(${entityName} param) {
        Boolean res = DataSourceExecute.businessRun(() -> {
            int count = mapper.updateByPrimaryKeySelective(param);
            return count > 0;
        });
        return res;
    }

    @Override
    public List<${entityName}> findEntityByTenantId(Long tenantId) {
        List<${entityName}> entitys = DataSourceExecute.businessRun(() -> {
            return mapper.findEntityByTenantId(tenantId);
        });
        return entitys;
    }

    // save or update 可能有用， 如果用不到请删除
    private Boolean updateByPrimaryKey(${entityName} param) {
        Boolean res = DataSourceExecute.businessRun(() -> {
            int count = mapper.updateByPrimaryKey(param);
            return count > 0;
        });
        return res;
    }
}